package com.xhj.xmly.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.R;
import com.xhj.xmly.adapter.ClassilyAdapter;
import com.xhj.xmly.adapter.Classily_FootAdapter;
import com.xhj.xmly.bean.Classily;
import com.xhj.xmly.bean.ClassilyAdavert;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Discover_classify_frag extends Fragment implements Callback<Classily> {
private List<Classily.ListBean> lists;
    private ClassilyAdapter adapter;

    private RecyclerView recyclerView;
    private ImageView head,foot;
    private View footView;
    private ViewPager footViewpager;
    private List<ImageView> viewList;

    public Discover_classify_frag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate( R.layout.fragment_discover_classify_frag,container, false);
        recyclerView=(RecyclerView)view.findViewById(R.id.classify_recycle);
        head = (ImageView)view.findViewById(R.id.head_image);
        footViewpager=(ViewPager)view.findViewById(R.id.classyly_viewpager_foot);
        init();
        footViewShow();
        return view;

    }



    public void init() {
        //下载数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/mobile/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.getClassily().enqueue(this);
        lists = new ArrayList<Classily.ListBean>();
        adapter=new ClassilyAdapter(lists,getContext());
        recyclerView.setAdapter(adapter);
        GridLayoutManager manager=new GridLayoutManager(getContext(),2);
//        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){
//            @Override
//            public int getSpanSize(int position) {
//                if (position==0)
//                {
//                    return 2;
//                }
//                return 1;
//            }
//        });
recyclerView.setLayoutManager(manager);

//间距
RecyclerView.ItemDecoration decoration=new RecyclerView.ItemDecoration(){
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int num = parent.getChildAdapterPosition(view);
        for (int i = 0; i <5 ; i++) {
            if (num == 4+(6*i) || num == 5+(6*i) ){

                outRect.set(0,0,0,20);
            }
        }

    }
};
        recyclerView.addItemDecoration(decoration);
    }
    private void footViewShow() {
         viewList=new ArrayList<>();
        Retrofit retrefit=new Retrofit.Builder().baseUrl("http://adse.ximalaya.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
                 HttpService service=retrefit.create(HttpService.class);
                 service.getClassilyFoot().enqueue(new Callback<ClassilyAdavert>() {
                     @Override
                     public void onResponse(Call<ClassilyAdavert> call, Response<ClassilyAdavert> response) {
                       for (int i=0;i<3;i++){
                           foot=new ImageView(getContext());
                           foot.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                   ViewGroup.LayoutParams.MATCH_PARENT));
                           foot.setScaleType(ImageView.ScaleType.FIT_XY);
                           foot.setImageResource(R.mipmap.ic_launcher);
                           String cover = response.body().getData().get(i).getCover();
                           Picasso.with(getContext()).load(cover).into(foot);
                           viewList.add(foot);
                           Classily_FootAdapter adapter=new Classily_FootAdapter(viewList);
                          // adapter.notifyDataSetChanged();
                           footViewpager.setAdapter(adapter);

                       }


                     }

                     @Override
                     public void onFailure(Call<ClassilyAdavert> call, Throwable t) {

                     }
                 });
    }

    @Override
    public void onResponse(Call<Classily> call, Response<Classily> response) {

        Classily classily=response.body();
        List<Classily.ListBean> list = classily.getList();
        for (int i = 1; i < list.size(); i++) {
            lists.add(list.get(i));
        }
//       lists.addAll(list);
//        Log.d("1620",list.get(1).getCoverPath());
//        Log.d("1620",list.get(0).getCoverPath());
        Picasso.with(getContext()).load(list.get(0).getCoverPath()).into(head);

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<Classily> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
